package com.cedancp.hometrade;

import library.UserFunctions;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.Toast;

public class BuscarPub extends Activity {
	
	Spinner spn_tipo;
	Spinner spn_ciudad;
	Spinner spn_precio;
	Button btn_buscar;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_buscar_pub);
		spn_tipo=(Spinner) findViewById(R.id.spn_tipo);
		spn_ciudad=(Spinner) findViewById(R.id.spn_ciudad_bsq);
		spn_precio=(Spinner) findViewById(R.id.spn_precio);
		btn_buscar=(Button) findViewById(R.id.btn_buscar);
		
		//populando spinners
		ArrayAdapter<CharSequence> tipoAdapter=ArrayAdapter.createFromResource(this, R.array.tipo, android.R.layout.simple_spinner_item);
		ArrayAdapter<CharSequence> ciudadAdapter=ArrayAdapter.createFromResource(this, R.array.ciudades, android.R.layout.simple_spinner_item);
		tipoAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		ciudadAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		spn_tipo.setAdapter(tipoAdapter);
		spn_ciudad.setAdapter(ciudadAdapter);
		
		spn_tipo.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> arg0, View arg1,
					int posicion, long arg3) {
				if (posicion==0) {
					ArrayAdapter<CharSequence> precioAdapter=ArrayAdapter.createFromResource(BuscarPub.this, R.array.precio_venta, android.R.layout.simple_spinner_item);
					precioAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
					spn_precio.setAdapter(precioAdapter);
				}
				if (posicion==1) {
					ArrayAdapter<CharSequence> precioAdapter=ArrayAdapter.createFromResource(BuscarPub.this, R.array.precio_arriendo, android.R.layout.simple_spinner_item);
					precioAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
					spn_precio.setAdapter(precioAdapter);
				}
				
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {
				// TODO Auto-generated method stub
				
			}
		});
		
		btn_buscar.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {				
					Intent i=new Intent(getApplicationContext(), ListaPublicaciones.class);
					i.putExtra("tipo_lista", "buscar");
					i.putExtra("tipo", spn_tipo.getSelectedItemPosition());
					i.putExtra("ciudad", spn_ciudad.getSelectedItemPosition());
					i.putExtra("precio", spn_precio.getSelectedItemPosition());
					startActivity(i);
					finish();
				
			}
		});
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.buscar_pub, menu);
		return true;
	}

	//Creando los eventos de las opciones de menu
	@Override
    public boolean onOptionsItemSelected(MenuItem item){
		UserFunctions userFunction= new UserFunctions();
    	switch (item.getItemId()) {
		case R.id.mnu_salir:
			if(userFunction.logoutUser(getApplicationContext())){
				Intent mainActivity=new Intent(getApplicationContext(), MainActivity.class);
				mainActivity.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
				startActivity(mainActivity);
			}
			
			return true;
			
		case R.id.mnu_cuenta:
				Intent iCuenta =new Intent(getApplicationContext(), AdministrarCuenta.class);
				startActivity(iCuenta);
				return true;
				
		case R.id.acercaDe:
			Intent iAcercaDe = new Intent(getApplicationContext(), AcercaDe.class);
			startActivity(iAcercaDe);
			return true;

		default:
			return super.onOptionsItemSelected(item);
		}
    }
}
